package com.mdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mdd.common.R;
import com.mdd.pojo.SeckillVoucher;
import com.mdd.pojo.Voucher;
import com.mdd.pojo.request.VoucherAddRequest;
import com.mdd.pojo.request.VoucherPageRequest;
import com.mdd.pojo.request.VoucherUpdateRequest;
import com.mdd.service.SeckillVoucherService;
import com.mdd.service.VoucherService;
import com.mdd.mapper.VoucherMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.mdd.content.RedisContent.SECKILL_ORDER_KEY_PREFIX;

/**
* @author 21958
* @description 针对表【voucher】的数据库操作Service实现
* @createDate 2023-04-03 21:14:37
*/
@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements VoucherService{

    @Autowired
    private SeckillVoucherService seckillVoucherService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 添加优惠券/秒杀券
     * @return
     */
    @Override
    @Transactional
    public R<String> addVoucher(VoucherAddRequest voucherAddRequest) {
        //解析voucherAdd请求体
        Voucher voucher = new Voucher();
        BeanUtils.copyProperties(voucherAddRequest,voucher);
        boolean b = save(voucher);
        //判断是否位秒杀券
        if (voucher.getType()!=1) {
            //添加优惠券
            return R.success("添加成功");
        }
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        //关联的优惠券id
        seckillVoucher.setVoucherId(voucher.getId());
        //库存
        seckillVoucher.setStock(voucherAddRequest.getStock());
        //生效时间
        seckillVoucher.setBeginTime(voucherAddRequest.getBeginTime());
        //结束时间
        seckillVoucher.setEndTime(voucherAddRequest.getEndTime());
        //添加秒杀券信息
        if (!seckillVoucherService.save(seckillVoucher)) {
            return R.error("添加失败");
        }
        //向redis中存储该秒杀券的库存
        redisTemplate.opsForValue().set(SECKILL_ORDER_KEY_PREFIX+voucher.getId(),voucherAddRequest.getStock().toString());
        return R.success("添加成功");
    }


    /**
     * 修改优惠券信息
     * @param voucherUpdateRequest
     * @return
     */
    @Override
    public R<String> updateVoucher(VoucherUpdateRequest voucherUpdateRequest) {
        //检验请求数据
        if (voucherUpdateRequest == null) {
            return R.error("没有数据");
        }
        //解析请求数据
        Voucher voucher = new Voucher();
        BeanUtils.copyProperties(voucherUpdateRequest,voucher);
        //修改优惠券信息
        if (!updateById(voucher)) {
            return R.error("修改失败");
        }
        return R.success("修改成功!");
    }

    /**
     * 优惠券分页
     * @param voucherPageRequest
     * @return
     */
    @Override
    public R<Page<Voucher>> voucherPage(VoucherPageRequest voucherPageRequest) {
        //检验请求数据
        if (voucherPageRequest == null) {
            return R.error("错误");
        }
        //创建分页对象
        Integer current = voucherPageRequest.getPage();
        Integer pageSize = voucherPageRequest.getPageSize();
        Page<Voucher> voucherPage = new Page<Voucher>(current,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Voucher> queryWrapper = new LambdaQueryWrapper<>();
        Long id = voucherPageRequest.getId();
        String title = voucherPageRequest.getTitle();
        String subTitle = voucherPageRequest.getSubTitle();
        Integer type = voucherPageRequest.getType();
        Integer status = voucherPageRequest.getStatus();
        queryWrapper.eq(id!=null,Voucher::getId,id)
                .like(title!=null,Voucher::getTitle,title )
                .like(subTitle!=null,Voucher::getSubTitle,subTitle)
                .eq(type!=null,Voucher::getType,type)
                .eq(status!=null,Voucher::getStatus,status);
        //分页查询
        Page<Voucher> page = page(voucherPage, queryWrapper);
        //返回数据
        return R.success(page);
    }
}




